//
//  HMHomeRecommendListVC.m
//  HowToDo
//
//  Created by Stan on 2017-01-03.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMHomeRecommendListVC.h"
#import <Masonry.h>
#import "HMRecommendListTVC.h"
#import "UIButton+JKImagePosition.h"



#pragma 定义了一个UITableView，并在这里面实现了代理方法。

typedef enum : NSUInteger {
    //  新到旧排序
    NewToOld,
    //  高到低排序
    HeightToLow
    
} SortType;
typedef enum: NSInteger {
    TitleTypeTime,
    TitleTypeHot
} TitleType;

//点击了选择排序类型之后，下拉出现的tableView

@interface SortSelectTableView : UITableView<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) UIView *(^sortBlock)(SortType type);


@end


@implementation SortSelectTableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.textLabel.font = [UIFont systemFontOfSize:k_FONT_NORMAL];
    
    if (indexPath.row == 0) {
        
        cell.textLabel.text = @"按上架时间（从新到旧）";
        return cell;
        
    }
    cell.textLabel.text = @"按7日内销量（从高到低）";
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        if (self.sortBlock) {
            UIView *view = self.sortBlock(NewToOld);
         
            [view removeFromSuperview];
        }
    }else {
        if (self.sortBlock) {
         UIView *view = self.sortBlock(HeightToLow);
            [view removeFromSuperview];
        }
    }
    
    [self removeFromSuperview];
    
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"enble" object:nil];
}
@end


#pragma 原本的Controller
@interface HMHomeRecommendListVC ()
//排序的View
@property(strong,nonatomic)UIView *sortHeaderView;

@property(strong,nonatomic)UIButton *sortBtn;

@property(strong,nonatomic)UIView *sortSelectView;

@property(strong,nonatomic)HMRecommendListTVC *recommendListTVC
;
@property (nonatomic, weak)void(^viewBlock)(UIView *view);

@end

@implementation HMHomeRecommendListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatSortHeaderView];
    
    //    包一层TableViewController，用来展示列表页
    
    self.recommendListTVC = [[HMRecommendListTVC alloc]init];
    //必须要在这里赋值，不然会无法正确刷新tableview
    self.recommendListTVC.programTypeID = self.programTypeID;
    
    [self addChildViewController:self.recommendListTVC];
    
    [self.view addSubview:self.recommendListTVC.tableView];
    
    [self.view layoutIfNeeded];
    [self.recommendListTVC.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sortHeaderView.mas_bottom).offset(0);
        make.left.bottom.right.offset(0);
    }];
    
//    设置页面导航栏的title
    
    if ([self.programTypeID isEqualToString:@(0).description]) {
        self.navigationItem.title = @"土豪专享";
    }else{
        self.navigationItem.title = @"十万个怎么办";
    }
    
    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByTwo];
    //-------------设置导航栏结束--------------------------------
}

/**
 加载头部按时间、销量排序的View
 */
- (void)creatSortHeaderView{
    self.sortHeaderView = [[UIView alloc]init];
    [self.view addSubview:self.sortHeaderView];
    self.sortHeaderView.backgroundColor = [UIColor whiteColor];
    
    [self.sortHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.right.offset(0);
        make.height.offset(54);
    }];
    
    [self.sortHeaderView layoutIfNeeded];
    
    UILabel *sortLabel = [UILabel hm_labelWithText:@"排序：" fontSize:k_FONT_NORMAL colorWithHex:0x686868];
    [self.sortHeaderView addSubview:sortLabel];
    //    sortLabel.text = @"排序";
    //    sortLabel.font = [UIFont systemFontOfSize:14];
    
    
    [sortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sortHeaderView.mas_top).offset(15);
        make.left.equalTo(self.sortHeaderView.mas_left).offset(20);
    }];
    
    UIButton *sortBtn = [[UIButton alloc]init];
    [self.sortHeaderView addSubview:sortBtn];
    self.sortBtn = sortBtn;
    sortBtn.titleLabel.font = [UIFont systemFontOfSize:k_FONT_NORMAL];
    
    //    按钮的默认状态
    [sortBtn setImage:[UIImage imageNamed:@"IQButtonBarArrowDown"] forState:UIControlStateNormal];
    [sortBtn hm_setTitle:@"按时间" andTiTleColorWithHex:0x686868 forState:UIControlStateNormal];
    

    [sortBtn sizeToFit];
    
    
    [sortBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sortLabel.mas_centerY).offset(0);
        make.right.equalTo(self.sortHeaderView.mas_right).offset(- 30);
    }];
    
    //    将按钮设置成左文右图
    [sortBtn jk_setImagePosition:LXMImagePositionRight spacing:10.0];
    
    
    [sortBtn addTarget:self action:@selector(sortBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineView = [[UIView alloc]init];
    
    [self.sortHeaderView addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHex:0xcbcbcb];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sortLabel.mas_bottom).offset(15);
        make.left.right.offset(0);
        make.height.offset(1);

    }];
}

//排序按钮点击事件
- (void)sortBtnClick:(UIButton *)sender{
  
        //        如果选中是NO，点击之后改变Button的选中状态。并创建view，在view中包裹一个tableview
        sender.enabled = NO;
        UIView *sortSelectView = [[UIView alloc]init];
        self.sortSelectView = sortSelectView;
        [self.view addSubview:sortSelectView];
        sortSelectView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        
        [sortSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.sortHeaderView.mas_bottom).offset(0);
            make.left.right.bottom.offset(0);
        }];
        
        //        创建自定义的UITableView
        SortSelectTableView *sortSelectTV = [[SortSelectTableView alloc]init];
        [sortSelectTV setSortBlock:^UIView*(SortType sortType) {
           
            if (sortType == NewToOld)
            {
                NSLog(@"%@", @"新到低");
                self.recommendListTVC.orderType = @"add_time";
                [self.sortBtn setTitle:@"按时间" forState:UIControlStateNormal];
            } else if (sortType == HeightToLow)
            {
                NSLog(@"%@", @"高到低");
                self.recommendListTVC.orderType = @"sold_count";
                  [self.sortBtn setTitle:@"按销量" forState:UIControlStateNormal];
                
            }
            
            return self.sortSelectView;
        }];
    //上拉
    __weak typeof(self)weakSElf = self;
        [self.recommendListTVC setNotificationToRequestBlock:^
     {
          
    
        }];
    
        [self.sortSelectView addSubview:sortSelectTV];
        sortSelectTV.rowHeight = 30;
        
        [sortSelectTV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0);
            make.height.offset(65);
        }];
        
        sortSelectTV.delegate = sortSelectTV;
        sortSelectTV.dataSource = sortSelectTV;
    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(btnState) name:@"enble" object:nil];
   
}
- (void)btnState {
    
    self.sortBtn.enabled = YES;
}

#pragma 重写方法
////重写父类栏目类型的方法
//- (void)setProgramTypeID:(NSString *)programTypeID{
//    [super setProgramTypeID:programTypeID];
//    
//    self.recommendListTVC.programTypeID = programTypeID;
//    
// 
//}

#pragma 懒加载
- (HMRecommendListTVC *)recommendListTVC{
    if (_recommendListTVC == nil) {
        _recommendListTVC = [[HMRecommendListTVC alloc]init];
    }
    return _recommendListTVC;
}


@end
